package kompoaufgabe9_steffen;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Steffen
 */
public class KompoAufgabe9_Steffen {

    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("KompoAufgabe9_SteffenPU");

    public KompoAufgabe9_Steffen() {
    }

    public void createTable(Object o) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(o);
            em.getTransaction().commit();
        } catch (Exception e) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }

            throw e;
        } finally {
            em.close();

        }
    }

    // je nach übergebener Reihe und String wird eine neue Reihe geupdated durch Methode persistChange
    public void updateColumn(int id,int column, String up) {
        System.out.println("Teste update von Tabelle "+id+" Reihe "+column+" mit String "+up);
        
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        CheckClass c = em.find(CheckClass.class, id);
        em.close();
        
        switch (column) {
            case 1:
                c.setString1(up);
                break;
            case 2:
                c.setString2(up);
                break;
            case 3:
                c.setString3(up);
                break;
            case 4:
                c.setString4(up);
                break;
            case 5:
                c.setString5(up);
                break;
            case 6:
                c.setString6(up);
                break;

        }
        persistChange(c);

    }

    public void persistChange(Object o) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.merge(o);
            em.getTransaction().commit();
        } catch (Exception e) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
                System.out.println("Exception geworfen: ");
                System.out.println(e.getMessage());
            //throw e;
        } finally {
            
            em.close();

        }
    }

    public void printTable(int id) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        em.refresh(em.find(CheckClass.class, id));
        System.out.println("Ergebnis:");
        System.out.println("Class ID: " + em.find(CheckClass.class, id).getId());
        System.out.println("String 1: " + em.find(CheckClass.class, id).getString1());
        System.out.println("String 2: " + em.find(CheckClass.class, id).getString2());
        System.out.println("String 3: " + em.find(CheckClass.class, id).getString3());
        System.out.println("String 4: " + em.find(CheckClass.class, id).getString4());
        System.out.println("String 5: " + em.find(CheckClass.class, id).getString5());
        System.out.println("String 6: " + em.find(CheckClass.class, id).getString6());

        em.close();
    }
    
    public void testInsertNull(int id){
        System.out.println("Fuege in alle Spalten von Tabelle 1 null ein...");
        for(int i = 1; i<=6;i++){
            updateColumn(id, i, null);
            printTable(1);
        }
        System.out.print("Ende Nulltest! \n\n");
    }
    
    public void testInsertUpdate(int id){
        for(int i = 1; i<=6;i++){
            String up = "update";
            up += Integer.toString(i);
            updateColumn(id, i, up);
            printTable(1);
        }
        System.out.print("Ende Updatetest! \n\n");
    }
    
    public void testUpdateUnique(int id){
        System.out.println("Versuche Einfuegen von gleichem String aus Tabelle 1 (nicht unique) in Zeile 3");
        updateColumn(2, 3, "check3.1");
        printTable(2);
        System.out.println("Versuche Einfuegen von gleichem String aus Tabelle 1 (unique) in Zeile 6");
        updateColumn(2, 6, "check6.1");
        printTable(2);
        System.out.println("Versuche Einfuegen von gleichem String aus Tabelle 1 (nicht unique) in Zele 6");
        updateColumn(2, 6, "update6.2");
        printTable(2);
    }

    public static void main(String[] args) {
        KompoAufgabe9_Steffen s = new KompoAufgabe9_Steffen();
        s.createTable(new CheckClass(1));
        s.createTable(new CheckClass(2));
        //s.testInsertNull(1);
        //s.testInsertUpdate(1);
        s.testUpdateUnique(2);
        
        
        
    }
}
